package com.tnovoselec.server.gdjejefrka.beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.interactive1.dbutils.DBUtil;
import com.interactive1.dbutils.Result;
import com.tnovoselec.server.gdjejefrka.Config;

public class Location extends DBBean {

	private int id;
	private double lat;
	private double lng;
	private int type;
	private long uid;
	private long timestamp;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLng() {
		return lng;
	}

	public void setLng(double lng) {
		this.lng = lng;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	@Override
	protected PreparedStatement getInsert(Connection con) throws SQLException {
		String sql = "INSERT INTO LOCATIONS (UID,LAT,LNG,LOCATION_TYPE,TIMESTAMP) VALUES (?,?,?,?,?)";

		PreparedStatement ps = con.prepareStatement(sql);
		ps.setLong(1, getUid());
		ps.setDouble(2, getLat());
		ps.setDouble(3, getLng());
		ps.setInt(4, getType());
		ps.setLong(5, getTimestamp());
		return ps;
	}

	@Override
	protected PreparedStatement getUpdate(Connection con) throws SQLException {
		String sql = "UPDATE LOCATIONS SET UID=?, LAT=?, LNG=?, LOCATION_TYPE=?, TIMESTAMP=? WHERE ID_LOCATION=?";

		PreparedStatement ps = con.prepareStatement(sql);
		ps.setLong(1, getUid());
		ps.setDouble(2, getLat());
		ps.setDouble(3, getLng());
		ps.setInt(4, getType());
		ps.setLong(5, getTimestamp());
		ps.setLong(6, getId());
		return ps;
	}

	@Override
	protected PreparedStatement getDelete(Connection con) throws SQLException {
		String sql = "DELETE FROM LOCATIONS WHERE ID_LOCATION=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setLong(1, getId());
		return ps;
	}

	public static Location createLocation(Result rs) {
		Location location = new Location();
		try {
			location.setId(rs.getInt("LOCATION_ID"));
			location.setLat(Double.parseDouble(rs.getString("LAT")));
			location.setLng(Double.parseDouble(rs.getString("LNG")));
			location.setType(rs.getInt("LOCATION_TYPE"));
			location.setUid(rs.getLong("UID"));
			location.setTimestamp(rs.getLong("TIMESTAMP"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return location;
	}

	public static List<Location> getReports(Location location){
		
		List<Location> locations = new ArrayList<Location>();
		double xFrom = location.getLng() - (Config.RADIUS / 111.2) / Math.cos(Math.toRadians(location.getLng()));
		double xTo = location.getLng() + (Config.RADIUS / 111.2) / Math.cos(Math.toRadians(location.getLng()));
		double yFrom = location.getLat() - Config.RADIUS / 111.2;
		double yTo = location.getLat() + Config.RADIUS / 111.2;
		long time = System.currentTimeMillis() - Config.TIMEFRAME;
		
		String sql = "SELECT * FROM LOCATIONS WHERE LNG BETWEEN " + xFrom + "  AND " + xTo + " AND LAT BETWEEN " + yFrom + " AND " + yTo + " AND TIMESTAMP> " + time;
		
		try {
			Result rs = DBUtil.select("gdjejefrka", sql);
			Location loc = null;
			while (rs.next()){
				loc = createLocation(rs);
				locations.add(loc);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		return locations;
		
	}
}
